package cn.chenxing.module.login.controller;

import cn.chenxing.domain.util.AlertUtil;
import cn.chenxing.domain.util.DialogBuilderUtil;
import cn.chenxing.domain.util.EnDecodeUtil;
import cn.chenxing.module.login.service.LoginService;
import cn.chenxing.module.user.entity.VenusUserInfoEntity;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Description 忘记密码控制层
 * @Author maogen.ymg
 * @Date 2020/3/6 11:39
 */
@Lazy
@FXMLController
@Slf4j
public class ForgetController implements Initializable {
    @FXML
    private JFXTextField userName;

    @FXML
    private JFXTextField mobilePhone;

    @FXML
    private JFXComboBox<String> securityQuestion;

    @FXML
    private JFXTextField securityAnswer;

    @FXML
    private JFXPasswordField password;

    @FXML
    private JFXPasswordField confirmPassword;

    @FXML
    private JFXButton forget;

    @Autowired
    private LoginService loginService;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        List<String> securityQuestionList = loginService.getSecurityQuestion();
        securityQuestion.getItems().addAll(securityQuestionList);
        securityQuestion.setValue(securityQuestionList.get(0));
    }

    @FXML
    private void submit(){
        // 填写内容检查
        if(!checkFilledInfo()) {
            return;
        }

        VenusUserInfoEntity userInfoEntity = new VenusUserInfoEntity();
        userInfoEntity.setUserName(userName.getText().trim())
                .setPassword(EnDecodeUtil.md5(password.getText().trim()))
                .setMobilePhone(mobilePhone.getText().trim())
                .setQuestionIndex(loginService.getQuestionIndex(securityQuestion.getValue()))
                .setAnswer(EnDecodeUtil.toBase64(securityAnswer.getText().trim()));

        // 检查填写的是否正确
        if(!loginService.checkFillCorrectness(userInfoEntity)) {
            DialogBuilderUtil.showPositiveAlert(forget,"请检查填写内容是否正确", "手机号码或问题答案不正确，请重新输入");
            return;
        }

        // 修改密码成功
        try {
            if (loginService.forgetUpdate(userInfoEntity)
                    && AlertUtil.showConfirmAlert("修改密码成功", "修改密码成功! 将回到登录界面")) {
                loginService.backLoginView(userName.getScene().getWindow());
            }
        }catch (Exception e){
            log.error("修改密码失败，原因是{}", e.getLocalizedMessage(), e);
            DialogBuilderUtil.showPositiveAlert(forget,"修改密码失败", "修改密码失败，请重试");
        }
    }

    @FXML
    private void back(){
        loginService.backLoginView(userName.getScene().getWindow());
    }

    private boolean checkFilledInfo(){
        // 完整性检查
        boolean isComplete = StringUtils.isNoneBlank(userName.getText(), password.getText(),
                confirmPassword.getText(), mobilePhone.getText(), securityAnswer.getText());
        if(!isComplete) {
            DialogBuilderUtil.showPositiveAlert(forget,"请检查!", "请完整填写账号和密码");
            return false;
        }

        // 格式检查

        if(!password.getText().equals(confirmPassword.getText())) {
            DialogBuilderUtil.showPositiveAlert(forget,"请注意密码填写", "两次输入的密码不一致");
            return false;
        }
        return true;
   }
}
